package mortal.game.manager
{
	import com.gengine.global.*;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import mortal.mvc.interfaces.*;
	
	public class PopupManager extends Object
	{
		private static var _topDisplayObject:DisplayObject;
		
		public function PopupManager()
		{
		}
		
		public static function addPopUp(param1:DisplayObject, param2:String = "window", param3:Boolean = false) : void
		{
			if (param1 && LayerManager.windowLayer.contains(param1) == false)
			{
				param1.x = (Global.stage.stageWidth - param1.width) / 2;
				param1.y = (Global.stage.stageHeight - param1.height) / 2;
				if (param2 == "window")
				{
					LayerManager.windowLayer.addChild(param1);
				}
				else if (param1 is IView)
				{
					(param1 as IView).show();
				}
				_topDisplayObject = param1;
				if (param3)
				{
					createTempCover(param1, 0, 0, true);
				}
			}
			
		}
		
		public static function centerPopup(param1:DisplayObject) : void
		{
			if (param1)
			{
				param1.x = (Global.stage.stageWidth - param1.width) / 2;
				param1.y = (Global.stage.stageHeight - param1.height) / 2;
			}
			
		}
		
		public static function isTop(param1:DisplayObject) : Boolean
		{
			return LayerManager.windowLayer.isTop(param1);
		}
		
		public static function removePopup(param1:DisplayObject) : void
		{
			if (LayerManager.windowLayer.contains(param1))
			{
				LayerManager.windowLayer.removeChild(param1);
				if (_topDisplayObject == param1)
				{
					_topDisplayObject = null;
				}
			}
			
		}
		
		public static function isPopup(param1:DisplayObject) : Boolean
		{
			return LayerManager.windowLayer.contains(param1);
		}
		
		public static function setTop(param1:DisplayObject) : void
		{
			if (_topDisplayObject == param1)
			{
				
			}
			_topDisplayObject = param1;
			LayerManager.windowLayer.setTop(param1);
			
		}
		
		private static function createTempCover(param1:DisplayObject, param2:uint = 0, param3:Number = 0.5, param4:Boolean = false) : void
		{
			var parent:DisplayObjectContainer;
			var back:Sprite;
			var removeHandler:Function;
			var v:* = param1;
			var color:* = param2;
			var alpha:* = param3;
			var mouseEnabled:* = param4;
			removeHandler = function (event:Event) : void
			{
				parent.removeEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
				if (parent.contains(back))
				{
					parent.removeChild(back);
				}
				
			}
				;
			parent = v.parent;
			back = new Sprite();
			var rect:* = localRectToContent(new Rectangle(0, 0, parent.stage.stageWidth, parent.stage.stageHeight), parent.stage, parent);
			back.graphics.beginFill(color, alpha);
			back.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
			back.graphics.endFill();
			back.mouseEnabled = mouseEnabled;
			parent.addChildAt(back, parent.getChildIndex(v));
			v.addEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
			
		}
		
		private static function localRectToContent(param1:Rectangle, param2:DisplayObject, param3:DisplayObject) : Rectangle
		{
			if (param2 == param3)
			{
				return param1;
			}
			var _loc_4:* = localToContent(param1.topLeft, param2, param3);
			var _loc_5:* = localToContent(param1.bottomRight, param2, param3);
			return new Rectangle(_loc_4.x, _loc_4.y, _loc_5.x - _loc_4.x, _loc_5.y - _loc_4.y);
		}
		
		public static function localToContent(param1:Point, param2:DisplayObject, param3:DisplayObject) : Point
		{
			if (param3 && param2)
			{
				return param3.globalToLocal(param2.localToGlobal(param1));
			}
			if (param2)
			{
				return param2.localToGlobal(param1);
			}
			if (param3)
			{
				return param3.globalToLocal(param1);
			}
			return null;
		}
		
		public static function closeTopWindow() : void
		{
			var windowLayer:* = LayerManager.windowLayer;
			if (windowLayer.numChildren == 0)
			{
				
			}
			var children:* = windowLayer.getChildAt((windowLayer.numChildren - 1)) as IView;
			if (children)
			{
				children.hide();
			}
			
		}
		
	}
}
